<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Application
 *
 * @author ananda
 */
abstract class App extends Kernel
{
    public $controller = null;
    public $action     = null;
    public $params     = array();

    public function __construct() {
        $this->init();
    }

    abstract public function init();
    abstract public function main();
    abstract public function route();

    public function run($options = null)
    {
        Com::glob('event')->trigger('application_start');
        $this->main($options);
        Com::glob('event')->trigger('application_end');
    }

    public function createController($controller, $parent = null)
    {
        // 先随便写写
        $conf = Com::glob('com.controllers');
        if (!isset($conf[$controller])) {
            throw new Exception_App("Controller [$controller] not exists");
        }
        $classname = "Controller_$controller";
        return new $classname($this, $parent);
    }

    public function createAction($controller, $action_name, $params = array())
    {
        // 先随便写写
        $controller->setAction($action_name, $params);
        $action = $controller->callAction();
        if (Error::is($action)) {
            throw new Exception_App("Action [{$controller->self_name}/{$action_name}] not exists");
        }
        return $action;
    }

}
